const mongoose = require('mongoose');

const hotelSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  description: {
    type: String,
    required: true
  },
  address: {
    type: String,
    required: true
  },
  city: {
    type: String,
    required: true
  },
  country: {
    type: String,
    required: true,
    default: '中国'
  },
  coordinates: {
    latitude: Number,
    longitude: Number
  },
  starRating: {
    type: Number,
    required: true,
    min: 1,
    max: 5
  },
  price: {
    type: Number,
    required: true,
    min: 0
  },
  originalPrice: {
    type: Number,
    min: 0
  },
  currency: {
    type: String,
    default: 'CNY'
  },
  images: [{
    type: String,
    required: true
  }],
  mainImage: {
    type: String,
    required: true
  },
  amenities: [{
    type: String
  }],
  roomTypes: [{
    name: String,
    price: Number,
    description: String,
    maxGuests: Number,
    amenities: [String]
  }],
  rating: {
    type: Number,
    default: 4.0,
    min: 0,
    max: 5
  },
  reviewCount: {
    type: Number,
    default: 0,
    min: 0
  },
  isAvailable: {
    type: Boolean,
    default: true
  },
  isPopular: {
    type: Boolean,
    default: false
  },
  checkInTime: {
    type: String,
    default: '14:00'
  },
  checkOutTime: {
    type: String,
    default: '12:00'
  },
  policies: [{
    type: String
  }],
  contact: {
    phone: String,
    email: String,
    website: String
  },
  tags: [{
    type: String
  }],
  distance: {
    airport: String,
    city_center: String,
    attractions: [{
      name: String,
      distance: String
    }]
  },
  discount: {
    type: String
  },
  bookingCount: {
    type: Number,
    default: 0,
    min: 0
  },
  
  // 订单相关属性
  allowBooking: {
    type: Boolean,
    default: true
  },
  minStayNights: {
    type: Number,
    default: 1
  },
  maxStayNights: {
    type: Number,
    default: 30
  },
  advanceBookingDays: {
    type: Number,
    default: 0 // 最少提前预订天数
  },
  cancellationPolicy: {
    type: String,
    default: '入住前24小时免费取消'
  },
  
  // 可用性管理
  totalRooms: {
    type: Number,
    default: 100
  },
  availableRooms: {
    type: Number,
    default: 100
  },
  
  // 预订统计
  totalBookings: {
    type: Number,
    default: 0
  },
  currentMonthBookings: {
    type: Number,
    default: 0
  }
}, {
  timestamps: true
});

// 创建索引
hotelSchema.index({ city: 1, starRating: -1 });
hotelSchema.index({ price: 1 });
hotelSchema.index({ rating: -1, reviewCount: -1 });
hotelSchema.index({ isPopular: -1, bookingCount: -1 });
hotelSchema.index({ name: 'text', description: 'text', city: 'text' });

module.exports = mongoose.model('Hotel', hotelSchema); 